<?php
	include ('../defines.php');
	
	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'tournaments.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
		)
	);

	$users = new Users();
	if (!$users->isLoggedUser())
		header('Location:' . ADMINISTRATOR_VIEW_PATH . 'login.php');

	$tournaments = new Tournaments();
	$arrTournaments = $tournaments->getAll();

	FunctionsHelper::includeTop();
?>

<div class="withBorder" id="main">
	<script type="text/javascript">
	$(document).ready(function(){
		$('.table').dataTable({
			"aoColumns": [
				null,
				null,
				null,
				null,
				null,
				null,
				{ "bSortable": false },
				{ "bSortable": false },
				{ "bSortable": false }
			]
		});
		$('.delete').live('click', function(){
			varUrl = '<?=ADMINISTRATOR_CONTROLLER_PATH; ?>operations.php';
			varData = { 'id' : this.id, 'operation' : 'delete', 'controller' : 'tournaments' };
			callAjax(varUrl, 'post', varData, funcSuccess);
		});
		$('.edit').live('click', function(){
			document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>tournament_am.php?id='+ this.id+'&action=m';
		});
		$('.activate').live('click', function(){
			varUrl = '<?=ADMINISTRATOR_CONTROLLER_PATH; ?>operations.php';
			varData = { 'id' : this.id, 'operation' : 'activate', 'controller' : 'tournaments' };
			callAjax(varUrl, 'post', varData, funcSuccess);
		});
		$('.create').click(function(){
			document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>tournament_am.php?action=a';
		});
		$('.addMatches').live('click', function(){
			document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>matches_am.php?tournament=' + this.id + '&sex='  + $(this).closest('tr').find('td.sex').html();
		});
	});
	</script>
	<div id="buttonAdd" class="buttonOperation create">
		<img title="Agregar" width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'add.png';?>" />
		<br />
		<span>NUEVO</span>
	</div>	
	<?php 
		if (!empty($arrTournaments)){
	?>
	<div class="tableContainer">
		<table class='table' id='tblTournaments'>
			<thead>
			<tr>
				<th>Torneo</th>
				<th>Sexo</th>
				<th>Fecha Inicial</th>
				<th>Fecha Final</th>
				<th>Tiene partidos</th>
				<th>Categor&iacute;a</th>
				<th class="tinyTable">Activo</th>
				<th class="tinyTable">Modificar</th>
				<th class="mediumTable">Activar/Desactivar</th>
			</tr>
			</thead>
			<tbody>
			<?php
			$cont=0;
			foreach ($arrTournaments as $tournament){
				$cont++;

				if ($cont%2==0)
					$class='even';
				else
					$class='odd';
			?>
			<tr>
				<td class='<?= $class?>'><?= $tournament->getName(); ?></td>
				<td class='sex <?= $class?>'><?= $tournament->getSex(); ?></td>
				<td class='<?= $class?>'><?= date('d/m/Y', strtotime($tournament->getInitialDate())); ?></td>
				<td class='<?= $class?>'><?= date('d/m/Y', strtotime($tournament->getFinalDate())); ?></td>
				<td class='<?= $class?>'><?= $tournament->getHasMatches() > 0 ? 'S&iacute;' : 'No'; ?><a href="javascript:void(0);"><img class="addMatches" title="Agregar partidos" id="<?= $tournament->getTournamentId();?>" width='15' height='15' src="<?=ADMINISTRATOR_IMAGE_PATH . 'add_mini.png';?>" /></a></td>
				<td class='<?= $class?>'><?= $tournament->getClass(); ?></td>
				<td class='<?= $class?>'><?= $tournament->getActive() ? 'S&iacute;' : 'No' ; ?></td>
				<td class='<?= $class?>'><a href='#' class='edit' id='<?= $tournament->getTournamentId(); ?>'><img width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'lapiz.png';?>" /></a></td>
				<td class='<?= $class?>'>
				<?
					if ($tournament->getActive()){
				?>
					<a href='#' class='delete buttonOperation' id='<?= $tournament->getTournamentId(); ?>'><img title="Desactivar" width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'trash.png';?>" /></a>
				<?php 
					}else{
				?>
					<a href='#' class='activate buttonOperation' id='<?= $tournament->getTournamentId(); ?>'><img title="Activar" width='30' height='30' src="<?=ADMINISTRATOR_IMAGE_PATH . 'active.png';?>" /></a>
				<?	
					}
				?>
				</td>
			</tr>
			<?php 
			}
			?>
			</tbody>
		</table>
	</div>
	<?php 
		}else{
	?>
	<div class='messageEmptyRows'><?php echo Messages::getMessage('EmptyRows');?></div>		
	<?php 		
		}
	?>
	<div class="clear"></div>
	<br />
	<img class="back backTable" src='<?= ADMINISTRATOR_IMAGE_PATH . 'back.png'; ?>' id="back" width='64' height='52' />
	<div class="clear"></div>
</div>
<?php 
	FunctionsHelper::includeBottom();
?>